home *** CD-ROM | disk | FTP | other *** search
- {
- This quick script can replace partial text in an existing XMP property
-
- The script asks for a XMP property from which parts of its text should be replaced.
- Then you can difine the old and new text.
-
- Author: Hertwig van Zwietering
- Initial Date: 2008-03-24
- Last changed: 2008-03-24
-
- Version: 1.0
- Last Changes: n.a.
-
- Version: 1.1
- Author: Hertwig van Zwietering
- Last Changes: Now also supports alternatives/sequences/bags/structures
- }
-
- var
- AProperty: String;
- AReplaceText: WideString;
- AReplaceWith: WideString;
-
- procedure AddParamToList (AParam: TMacroParam; AList: TList);
- begin
- AList.Add (AParam);
-
- if (AParam.ParamType = ptAlternative) or
- (AParam.ParamType = ptBag) or
- (AParam.ParamType = ptSequence)
- then
- begin
- for i := 0 to AParam.ArrayContent.Count - 1 do
- AddParamToList (AParam.ArrayContent.Items[i], AList);
- end;
-
- if (AParam.ParamType = ptStructure) then
- begin
- for i := 0 to AParam.CustomParams.Count - 1 do
- AddParamToList (AParam.CustomParams.Items[i], AList);
- end;
- end;
-
- function HandleImage (AImage: TImageItem): Boolean;
- var
- ACatItem: TCatalogItem;
- AXmp: TXMP;
- AParam: TMacroParam;
- AList: TList;
- i: Integer;
- AHit: Integer;
- begin
- result := False;
-
- ACatItem := TCatalogItem.Create(nil);
- if Catalog.FindImageCombined (AImage, ACatItem, False, vptNone) then
- begin
- AXmp := TXMP.Create (False);
-
- Catalog.LoadXMPForItem (ACatItem, AXmp, Options.CachedXMP);
-
- AParam := AXmp.XMPDesign.FindCommand (AProperty, True);
- if AParam <> nil then
- begin
- AList := TList.Create;
-
- AddParamToList (AParam, AList);
-
- AHit := 0;
- for i := 0 to AList.Count - 1 do
- begin
- AParam := AList.Items[i];
-
- if (AParam.ParamContent <> Null) and (Nvl(AParam.ParamContent, '') <> '') then
- begin
- // anything to replace?
- if WideTextPos (AReplaceText, AParam.ParamContent) > 0 then
- begin
- AParam.ParamContent := StrTran (AParam.ParamContent, AReplaceText, AReplaceWith);
-
- Inc (AHit);
- end;
- end;
- end;
- AList.Free;
-
- if AHit > 0 then
- begin
- // store XMP again
- Catalog.SaveXMPForItem (ACatItem, AXmp, Options.CachedXMP);
- result := True;
- end;
- end;
-
- AXmp.Free;
- end;
-
- ACatItem.Free;
- end;
-
- procedure _OnPopupSelected (Sender: TObject);
- var
- APopup: TXMPSelectorPopup;
- AEdit: TEdit;
- begin
- APopup := Sender;
- AEdit := APopup.Component;
-
- AEdit.Text := APopup.SelectedText;
- end;
-
- procedure _OnPopupClick (Sender: TObject);
- var
- AButton: TSpeedButton;
- APopup: TXMPSelectorPopup;
- begin
- AButton := Sender;
- APopup := AButton.Tag;
-
- APopup.Popup (Mouse.CursorPos.X, Mouse.CursorPos.Y);
- end;
-
- function AskSettings: Boolean;
- var
- MyForm: TForm;
- AProp: TEdit;
- AText: TEdit;
- AWith: TEdit;
- AButton: TButton;
- ALabel: TLabel;
- APopup: TXMPSelectorPopup;
- APopupButton: TSpeedButton;
- begin
- result := False;
-
- AProperty := ReadFromRegistry ('Scripts\PartialReplace', 'Property', 'photoshop:headline');
- AReplaceText := ReadFromRegistry ('Scripts\PartialReplace', 'ReplaceText', '<old text>');
- AReplaceWith := ReadFromRegistry ('Scripts\PartialReplace', 'ReplaceWith', '<new text>');
-
- MyForm := TForm.Create(nil);
- MyForm.Position := poScreenCenter;
- MyForm.BorderStyle := bsDialog;
- MyForm.Height := 170;
-
-
- ALabel := TLabel.Create(MyForm);
- ALabel.Parent := MyForm;
- ALabel.Caption := 'XMP property';
- ALabel.Top := 13;
- ALabel.Left := 5;
- AProp := TEdit.Create(MyForm);
- AProp.Parent := MyForm;
- AProp.Text := AProperty;
- AProp.Top := 10;
- AProp.Left := 100;
- APopup := TXMPSelectorPopup.Create (MyForm);
- APopup.OnSelected := '_OnPopupSelected';
- APopup.Component := AProp;
- APopupButton := TSpeedButton.Create (MyForm);
- APopupButton.Parent := MyForm;
- APopupButton.Top := AProp.Top;
- APopupButton.Left := AProp.Left + AProp.Width + 2;
- APopupButton.Caption := '...';
- APopupButton.Tag := APopup;
- APopupButton.OnClick := '_OnPopupClick';
- AProp.PopupMenu := APopup;
-
- ALabel := TLabel.Create(MyForm);
- ALabel.Parent := MyForm;
- ALabel.Caption := 'Text to replace';
- ALabel.Top := 43;
- ALabel.Left := 5;
- AText := TEdit.Create(MyForm);
- AText.Parent := MyForm;
- AText.Text := AReplaceText;
- AText.Top := 40;
- AText.Left := 100;
-
- ALabel := TLabel.Create(MyForm);
- ALabel.Parent := MyForm;
- ALabel.Caption := 'Replace with';
- ALabel.Top := 73;
- ALabel.Left := 5;
- AWith := TEdit.Create(MyForm);
- AWith.Parent := MyForm;
- AWith.Text := AReplaceWith;
- AWith.Top := 70;
- AWith.Left := 100;
-
-
- AButton := TButton.Create(MyForm);
- AButton.Parent := MyForm;
- AButton.Default := True;
- AButton.ModalResult := mrOk;
- AButton.Caption := 'OK';
- AButton.Top := MyForm.ClientHeight - 5 - AButton.Height;
- AButton.Left := 5;
-
- AButton := TButton.Create(MyForm);
- AButton.Parent := MyForm;
- AButton.Default := True;
- AButton.ModalResult := mrCancel;
- AButton.Caption := 'Cancel';
- AButton.Top := MyForm.ClientHeight - 5 - AButton.Height;
- AButton.Left := 5 + AButton.Width + 5;
-
-
- MyForm.ShowModal;
-
- if MyForm.ModalResult = mrOk then
- begin
- if (Trim(AText.Text) = '') or (Trim(AProp.Text) = '') then
- Say ('No property or replace text specified.')
- else
- begin
- AProperty := AProp.Text;
- AReplaceText := AText.Text;
- AReplaceWith := AWith.Text;
-
- WriteToRegistry ('Scripts\PartialReplace', 'Property', AProperty);
- WriteToRegistry ('Scripts\PartialReplace', 'ReplaceText', AReplaceText);
- WriteToRegistry ('Scripts\PartialReplace', 'ReplaceWith', AReplaceWith);
-
- result := True;
- end;
- end;
-
- MyForm.Free;
- end;
-
- var
- i: Integer;
- AHit: Integer;
- begin
- if not AskSettings then
- exit;
-
- Progress.Cancel := False;
- Progress.UseProgress;
- Progress.Max := Selected.Count;
- Progress.Pos := 0;
- Progress.Show;
-
- AHit := 0;
- for i := 0 to Selected.Count - 1 do
- begin
- Progress.Pos := i + 1;
- Progress.ProgressText := Selected.Items[i].FileNameOnly;
-
- if HandleImage (Selected.Items[i]) then
- Inc (AHit);
-
- if Progress.Cancel then
- break;
- end;
-
- Progress.Hide;
-
- if Progress.Cancel then
- Say (WideFormat('Cancelled; %d handled', [AHit]))
- else
- SayOnce ('PartialXMPScript', WideFormat('Finished; %d handled', [AHit]), 0);
-
- if AHit > 0 then
- begin
- RefreshPanels;
- InvalidateCollection;
- end;
- end;
-